package com.hzhc.controller.sysmanage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.RoleSelectVo;
import com.hzhc.entity.SysRole;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.SysRoleMenuService;
import com.hzhc.service.SysRoleService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.SysRoleVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 功不唐捐
 * @since 2024-04-07
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sysRole")
public class SysRoleController extends BaseController {

    private final SysRoleService roleService;

    private final SysRoleMenuService roleMenuService;

    /**
     * code校验
     * @param request
     * @param sysRole
     * @return
     */
    @RequestMapping("/getRoleCode")
    public ResultUtil<T> getRoleCode(HttpServletRequest request, @RequestBody SysRole sysRole){
        return roleService.getRoleCode(sysRole);
    }

    /**
     * 判断code是否存在
     * @param code
     * @return
     */
    @RequestMapping("/selectRoleCode/{code}")
    public ResultUtil<T> selectRoleCode(@PathVariable("code") String code){
        return roleService.selectRoleCode(code);
    }

    /**
     * 判断code是否存在
     * @param code
     * @return
     */
    @RequestMapping("/selectRoleCode/{type}/{code}")
    public ResultUtil<T> selectRoleCode(@PathVariable("type") String type,@PathVariable("code") String code){
        return roleService.selectRoleCode(type,code);
    }

    /**
     * 角色列表
     */
    @RequestMapping("/getRoleList")
    public ResultUtil<List<SysRoleVo>> getRoleList(@RequestBody SysRoleVo vo) {
        Page<SysRoleVo> page = new Page(vo.getPageIndex(), vo.getPageSize());
        List<SysRoleVo> list = roleService.getRolePage(page, vo);
        return ResultUtil.build(list,page.getTotal(), page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }
    /**
     * 获取所有角色列表
     *
     * @param request
     * @return
     */
    @RequestMapping("/getAllRoleList")
    public ResultUtil<List<SysRole>> getUserList(HttpServletRequest request, @RequestBody SysRole datas) {
        return ResultUtil.build(roleService.getRoleList(datas), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 角色新增
     */
    @RequestMapping("/saveRole")
    @SysTemLog(operModul = "角色管理-角色新增", operType = "新增", operDesc = "角色新增功能", modular = "PC")
    public ResultUtil<T> saveRole(@RequestBody SysRole role) {
        return roleService.saveRole(role);
    }

    /**
     * 角色编辑
     */
    @RequestMapping("/updateRole")
    @SysTemLog(operModul = "角色管理-角色修改", operType = "编辑", operDesc = "角色修改功能", modular = "PC")
    public ResultUtil<T> updateRole(@RequestBody SysRole role) {
        return roleService.updateRole(role);
    }

    /**
     * 角色删除
     */
    @RequestMapping("/deleteRole")
    @SysTemLog(operModul = "角色管理-角色删除", operType = "删除", operDesc = "角色删除功能", modular = "PC")
    public ResultUtil<T> deleteRole(@RequestBody SysRole role) {
        return roleService.deleteRole(role);
    }

    /**
     * 角色校验
     */
    @RequestMapping("/checkRole")
    public ResultUtil<T> checkRole(@RequestBody SysRole role) {
        return roleService.checkRole(role);
    }

    /**
     * 角色授权回显
     */
    @RequestMapping("/getRoleMenuList")
    public ResultUtil<List<Long>> getRoleMenuList(@RequestBody SysRoleVo vo) {
        List<Long> roleMenu = roleMenuService.getRoleMenuList(vo);
        return ResultUtil.build(roleMenu, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 角色授权
     */
    @RequestMapping("/updateRoleMenu")
    @SysTemLog(operModul = "角色管理-角色授权", operType = "授权", operDesc = "角色授权功能", modular = "PC")
    public ResultUtil<T> updateRoleMenu(@RequestBody SysRoleVo vo) {
        return roleMenuService.updateRoleMenu(vo);
    }

    /**
     * 查询角色
     * @return
     */
    @RequestMapping("/getAllRoleByUserId")
    public ResultUtil<List<RoleSelectVo>> getAllRoleByUserIdList(@RequestBody SysRoleVo vo) {
        List<RoleSelectVo> allRoleByUserIdList = roleService.getAllRoleByUserIdList(vo);
        return ResultUtil.build(allRoleByUserIdList, ResultUtilCodeEnum.SUCCESS);
    }
}
